package fast.mvc;

import fast.calc.Buffs;
import fast.data.CharacterClass;
import fast.loader.GlyphDB;
import fast.loader.Profession;
import fast.loader.Race;
import fast.loader.TalentDB;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JToggleButton;

public class PlayerPanel extends JPanel {
	
	private static final long serialVersionUID = 852930853916977746L;
	
	private JComboBox<Race> raceList;
	private JComboBox<Profession> professionList;
	private JComboBox<Profession> professionList2;
	private ButtonGroup[] talent_tree = new ButtonGroup[6];
	private Set<JToggleButton> talents = new HashSet<JToggleButton>();
	private Set<JCheckBox> glyphs = new HashSet<JCheckBox>();
	private Set<JCheckBox> buffs = new HashSet<JCheckBox>();
	
	public PlayerPanel(CharacterClass cc,Race race,List<Profession> professions,List<String> glyphs,Set<String> talentset){
		//this.setLayout(null);
		this.setLayout(new BorderLayout());
		
		JTabbedPane tabbedPane = new JTabbedPane();
		add(tabbedPane);
		//tabbedPane.setBounds(0, 0, this.getWidth(), this.getWidth());
		
		JPanel player = new JPanel();
		player.setLayout(null);
		
		JLabel labelRace = new JLabel("Race:");
		labelRace.setBounds(10, 10, 150, 20);
		player.add(labelRace);
		
		raceList = new JComboBox<Race>(Race.values());
		raceList.setBounds(10, 30, 150, 30);
		raceList.setSelectedItem(race);
		player.add(raceList);
		
		JLabel labelProfession = new JLabel("Professions:");
		labelProfession.setBounds(10, 70, 150, 20);
		player.add(labelProfession);
		
		professionList = new JComboBox<Profession>(Profession.values());
		professionList.setBounds(10, 90, 150, 30);
		professionList.setSelectedItem(professions.get(0));
		player.add(professionList);
		
		professionList2 = new JComboBox<Profession>(Profession.values());
		professionList2.setBounds(170, 90, 150, 30);
		professionList2.setSelectedItem(professions.get(1));
		player.add(professionList2);
		
		JLabel labelTalents = new JLabel("Talents:");
		labelTalents.setBounds(10, 130, 150, 20);
		player.add(labelTalents);
		
	    TalentDB talentDB = new TalentDB(cc);
	    for(int i = 0;i < 6;i++){
	    	talent_tree[i] = new ButtonGroup();
	    	for(int j = 0;j < 3;j++){
	    		String talent_name = talentDB.get(i, j);
	    		JToggleButton talButton = new JToggleButton(talent_name);
	    		if(talentset.contains(talent_name))
	    			talButton.setSelected(true);
	    		talButton.setActionCommand(talent_name);
	    		talButton.setBounds(10+j*100, 150+i*30, 100, 30);
	    		talButton.setFont(new Font("sansserif",Font.PLAIN,11));
	    		talButton.setMargin(new Insets(0, 0, 0, 0));
	    		talents.add(talButton);
	    		player.add(talButton);
	    	    talent_tree[i].add(talButton);
	    	}
	    }
		
		tabbedPane.addTab("Player", player);
		
		JPanel glyphTab = new JPanel();
		glyphTab.setLayout(new GridLayout(0,2,0,0));
		GlyphDB glyphDB = new GlyphDB();
		for(Integer glyph : glyphDB.getAll(cc).keySet()){
			JCheckBox buffbox = new JCheckBox(glyphDB.getGlyph(glyph).getName());
			buffbox.setFont(new Font("sansserif",Font.PLAIN,10));
			buffbox.setMargin(new Insets(0,0,0,0));
			if(glyphs.contains(glyphDB.getGlyph(glyph).getName()))
				buffbox.setSelected(true);
			else
				buffbox.setSelected(false);
			this.glyphs.add(buffbox);
			glyphTab.add(buffbox);
		}
		
		JScrollPane glyphScrollPane = new JScrollPane();
		glyphScrollPane.setViewportView(glyphTab);
		tabbedPane.addTab("Glyphs", glyphScrollPane);
		
		JPanel buffsTab = new JPanel();
		JScrollPane buffsScrollPane = new JScrollPane(buffsTab);
		buffsTab.setLayout(new GridLayout(0,1,0,0));
		for(String buff : Buffs.validBuffs){
			JCheckBox buffbox = new JCheckBox(buff);
			buffbox.setSelected(true);
			this.buffs.add(buffbox);
			buffsTab.add(buffbox);
		}
		
		tabbedPane.addTab("Buffs", buffsScrollPane);
	}
	
	public void addRaceChangeListener(ActionListener l){
        raceList.addActionListener(l);
    }
	
	public void addProfessionChangeListener(ActionListener l){
		professionList.addActionListener(l);
		professionList2.addActionListener(l);
	}
	
	public void addGlyphChangeListener(ActionListener l){
		for(JCheckBox glyph : glyphs){
			glyph.addActionListener(l);
		}
	}
	
	public void addBuffChangeListener(ActionListener l){
		for(JCheckBox buff : buffs){
			buff.addActionListener(l);
		}
	}
	
	public void addTalentChangeListener(ActionListener l){
		for(JToggleButton btn : talents){
			btn.addActionListener(l);
		}
	}
	
	public List<Profession> getSelectedProfessions(){
		List<Profession> pro = new ArrayList<Profession>();
		pro.add((Profession) professionList.getSelectedItem());
		pro.add((Profession) professionList2.getSelectedItem());
		return pro;
	}

	public Set<String> getTalentTree() {
		Set<String> ss = new HashSet<String>();
		for(ButtonGroup bg : talent_tree){
			if(bg.getSelection() != null)
				ss.add(bg.getSelection().getActionCommand());
		}
		return ss;
	}
}
